

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="若发现文章有误，敬请指正，感谢@[TOC]  参考资料 视频链接 一、运行环境  Vmware CentOS 7 操作系统 JDK 8 MySQL8 Hadoop3.3.0（单节点） HIve 3.1.2 on YARN  节点分配：![在这里插入图片描述](https:&#x2F;&#x2F;img-blog.csdnimg.cn&#x2F;db704f9ef9a54fee864d78453881e5a5.png# &amp;#x">
<meta property="og:type" content="article">
<meta property="og:title" content="Hive 笔记2 使用窗口函数over实现电商常见五个需求_ 查询某个时期购买过的顾客以及总人数_ 顾客购买明细以及总额 _ 按照日期对花费进行累加 _查询顾客上次购买时间 _ 查询前20%时间">
<meta property="og:url" content="http://example.com/2022/03/25/Hive%20%E7%AC%94%E8%AE%B02%20%E4%BD%BF%E7%94%A8%E7%AA%97%E5%8F%A3%E5%87%BD%E6%95%B0over%E5%AE%9E%E7%8E%B0%E7%94%B5%E5%95%86%E5%B8%B8%E8%A7%81%E4%BA%94%E4%B8%AA%E9%9C%80%E6%B1%82_%20%E6%9F%A5%E8%AF%A2%E6%9F%90%E4%B8%AA%E6%97%B6%E6%9C%9F%E8%B4%AD%E4%B9%B0%E8%BF%87%E7%9A%84%E9%A1%BE%E5%AE%A2%E4%BB%A5%E5%8F%8A%E6%80%BB%E4%BA%BA%E6%95%B0_%20%E9%A1%BE%E5%AE%A2%E8%B4%AD%E4%B9%B0%E6%98%8E%E7%BB%86%E4%BB%A5%E5%8F%8A%E6%80%BB%E9%A2%9D%20_%20%E6%8C%89%E7%85%A7%E6%97%A5%E6%9C%9F%E5%AF%B9%E8%8A%B1%E8%B4%B9%E8%BF%9B%E8%A1%8C%E7%B4%AF%E5%8A%A0%20_%E6%9F%A5%E8%AF%A2%E9%A1%BE%E5%AE%A2%E4%B8%8A%E6%AC%A1%E8%B4%AD%E4%B9%B0%E6%97%B6%E9%97%B4%20_%20%E6%9F%A5%E8%AF%A2%E5%89%8D20%%E6%97%B6%E9%97%B4/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="若发现文章有误，敬请指正，感谢@[TOC]  参考资料 视频链接 一、运行环境  Vmware CentOS 7 操作系统 JDK 8 MySQL8 Hadoop3.3.0（单节点） HIve 3.1.2 on YARN  节点分配：![在这里插入图片描述](https:&#x2F;&#x2F;img-blog.csdnimg.cn&#x2F;db704f9ef9a54fee864d78453881e5a5.png# &amp;#x">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/24c3b8a9259644e39061e2e880853cf9.png">
<meta property="article:published_time" content="2022-03-25T13:15:22.000Z">
<meta property="article:modified_time" content="2022-08-22T15:44:45.105Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Hive">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/24c3b8a9259644e39061e2e880853cf9.png">
  
  
  
  <title>Hive 笔记2 使用窗口函数over实现电商常见五个需求_ 查询某个时期购买过的顾客以及总人数_ 顾客购买明细以及总额 _ 按照日期对花费进行累加 _查询顾客上次购买时间 _ 查询前20%时间 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Hive 笔记2 使用窗口函数over实现电商常见五个需求_ 查询某个时期购买过的顾客以及总人数_ 顾客购买明细以及总额 _ 按照日期对花费进行累加 _查询顾客上次购买时间 _ 查询前20%时间"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-03-25 21:15" pubdate>
          2022年3月25日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          2.7k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          23 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Hive 笔记2 使用窗口函数over实现电商常见五个需求_ 查询某个时期购买过的顾客以及总人数_ 顾客购买明细以及总额 _ 按照日期对花费进行累加 _查询顾客上次购买时间 _ 查询前20%时间</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <blockquote>
<p>若发现文章有误，敬请指正，感谢<br>@[TOC]</p>
</blockquote>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><hr>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1EZ4y1G7iL?p=75">视频链接</a></p>
<h1 id="一、运行环境"><a href="#一、运行环境" class="headerlink" title="一、运行环境"></a>一、运行环境</h1><hr>
<ul>
<li>Vmware</li>
<li>CentOS 7 操作系统</li>
<li>JDK 8</li>
<li>MySQL8</li>
<li>Hadoop3.3.0（单节点）</li>
<li>HIve 3.1.2 on YARN</li>
</ul>
<p>节点分配：<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/db704f9ef9a54fee864d78453881e5a5.png#">https://img-blog.csdnimg.cn/db704f9ef9a54fee864d78453881e5a5.png#</a> &#x3D;50%x)</p>
<p>其中MySQL和Hive都安装在hadoop101节点</p>
<h1 id="二、准备测试数据"><a href="#二、准备测试数据" class="headerlink" title="二、准备测试数据"></a>二、准备测试数据</h1><hr>
<p>客户的消费情况（客户昵称、消费日期、消费金额）<code>cost.txt</code></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">jack</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">01</span>,<span class="hljs-number">10</span><br><span class="hljs-attribute">tony</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>,<span class="hljs-number">15</span><br><span class="hljs-attribute">jack</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">02</span>-<span class="hljs-number">03</span>,<span class="hljs-number">23</span><br><span class="hljs-attribute">tony</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">04</span>,<span class="hljs-number">29</span><br><span class="hljs-attribute">jack</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">05</span>,<span class="hljs-number">46</span><br><span class="hljs-attribute">jack</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">06</span>,<span class="hljs-number">42</span><br><span class="hljs-attribute">tony</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">07</span>,<span class="hljs-number">50</span><br><span class="hljs-attribute">jack</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">08</span>,<span class="hljs-number">55</span><br><span class="hljs-attribute">mart</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">08</span>,<span class="hljs-number">62</span><br><span class="hljs-attribute">mart</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">09</span>,<span class="hljs-number">68</span><br><span class="hljs-attribute">neil</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">05</span>-<span class="hljs-number">10</span>,<span class="hljs-number">12</span><br><span class="hljs-attribute">mart</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">11</span>,<span class="hljs-number">75</span><br><span class="hljs-attribute">neil</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">06</span>-<span class="hljs-number">12</span>,<span class="hljs-number">80</span><br><span class="hljs-attribute">mart</span>,<span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">13</span>,<span class="hljs-number">94</span><br></code></pre></td></tr></table></figure>

<p>将该文本数据保存到本地，等会加载到Hive数据表</p>
<p>进入Hive Shell，创建测试的表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs sql">hive (<span class="hljs-keyword">default</span>)<span class="hljs-operator">&gt;</span> <span class="hljs-keyword">create</span> <span class="hljs-keyword">table</span> testover(<br>              <span class="hljs-operator">&gt;</span> name string,<br>              <span class="hljs-operator">&gt;</span> orderdate string,<br>              <span class="hljs-operator">&gt;</span> cost <span class="hljs-type">int</span>)<br>              <span class="hljs-operator">&gt;</span> <span class="hljs-type">ROW</span> FORMAT DELIMITED FIELDS TERMINATED <span class="hljs-keyword">BY</span> <span class="hljs-string">&#x27;,&#x27;</span>;<br></code></pre></td></tr></table></figure>

<p><strong>加载本地数据</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs shell">hive (default)&gt; load data local inpath &quot;/opt/module/hive/data/cost.txt&quot; into table testover;<br><br>hive (default)&gt; select * from testover;<br></code></pre></td></tr></table></figure>
<p><strong>查询结果：</strong><br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/c7026cbf00654a108dbf5fb413c8ab6b.png#">https://img-blog.csdnimg.cn/c7026cbf00654a108dbf5fb413c8ab6b.png#</a> &#x3D;60%x)</p>
<h1 id="三、OVER-窗口函数"><a href="#三、OVER-窗口函数" class="headerlink" title="三、OVER() 窗口函数"></a>三、OVER() 窗口函数</h1><hr>
<p><strong>OVER()：</strong> 指定分析函数工作的数据窗口大小，这个数据窗口大小可能会随着行的变而变化</p>
<p>常用参数有：</p>
<table>
<thead>
<tr>
<th align="left">参数名</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">CURRENT ROW</td>
<td align="left">当前行</td>
</tr>
<tr>
<td align="left">n PRECEDING</td>
<td align="left">往前 n 行数据</td>
</tr>
<tr>
<td align="left">n FOLLOWING</td>
<td align="left">往后 n 行数据</td>
</tr>
<tr>
<td align="left">UNBOUNDED</td>
<td align="left">起点 ，后面通常要接PRECEDING或者FOLLOWING</td>
</tr>
<tr>
<td align="left">UNBOUNDED PRECEDING</td>
<td align="left">表示从前面的起点</td>
</tr>
<tr>
<td align="left">UNBOUNDED FOLLOWING</td>
<td align="left">表示到后面的终点</td>
</tr>
<tr>
<td align="left">LAG(col,n,default_val)</td>
<td align="left">往前第 n 行数据</td>
</tr>
<tr>
<td align="left">LEAD(col,n, default_val)</td>
<td align="left">往后第 n 行数据</td>
</tr>
<tr>
<td align="left">NTILE(n)</td>
<td align="left">把有序窗口的行分发到指定数据的组中，各个组有编号，编号从 1 开始，对 于每一行，NTILE 返回此行所属的组的编号。注意：n 必须为 int 类型。</td>
</tr>
</tbody></table>
<p><strong>标准语法：</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql">分析函数 <span class="hljs-keyword">over</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> 列名 <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> 列名 <span class="hljs-keyword">rows</span> <span class="hljs-keyword">between</span> 开始位置 <span class="hljs-keyword">and</span> 结束位置)<br></code></pre></td></tr></table></figure>

<h1 id="需求一：查询在某年某月购买过商品的顾客以及总人数"><a href="#需求一：查询在某年某月购买过商品的顾客以及总人数" class="headerlink" title="需求一：查询在某年某月购买过商品的顾客以及总人数"></a>需求一：查询在某年某月购买过商品的顾客以及总人数</h1><hr>
<p>这里以2017年4月份为例</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> <br>    name, <br>    <span class="hljs-built_in">count</span>(<span class="hljs-operator">*</span>) <span class="hljs-keyword">over</span>() person_count <br><span class="hljs-keyword">FROM</span> testover<br><span class="hljs-keyword">WHERE</span> <span class="hljs-built_in">substring</span>(orderdate, <span class="hljs-number">1</span>, <span class="hljs-number">7</span>) <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;2017-04&#x27;</span><br><span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> name;<br></code></pre></td></tr></table></figure>

<p><strong>运行结果：</strong></p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">name</span>	person_count<br><span class="hljs-attribute">mart</span>	<span class="hljs-number">2</span><br><span class="hljs-attribute">jack</span>	<span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>

<h1 id="需求二：查询顾客的购买明细以及月购买总额"><a href="#需求二：查询顾客的购买明细以及月购买总额" class="headerlink" title="需求二：查询顾客的购买明细以及月购买总额"></a>需求二：查询顾客的购买明细以及月购买总额</h1><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span><br>    name,<br>    orderdate,<br>    cost,<br>    <span class="hljs-built_in">sum</span>(cost) <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">month</span>(orderdate))<br><span class="hljs-keyword">FROM</span> testover;<br></code></pre></td></tr></table></figure>

<p><strong>查询结果：</strong><br><img src="https://img-blog.csdnimg.cn/24c3b8a9259644e39061e2e880853cf9.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="需求三：将每个顾客的花费按照日期进行累加"><a href="#需求三：将每个顾客的花费按照日期进行累加" class="headerlink" title="需求三：将每个顾客的花费按照日期进行累加"></a>需求三：将每个顾客的花费按照日期进行累加</h1><hr>
<h2 id="3-1-按名称分组统计花费"><a href="#3-1-按名称分组统计花费" class="headerlink" title="3.1  按名称分组统计花费"></a>3.1  按名称分组统计花费</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span><br>    name,orderdate,cost,<br>    <span class="hljs-built_in">SUM</span>(cost) <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> name)<br><span class="hljs-keyword">FROM</span> testover;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong><br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/1ea8f6150a2b4ff3999a9d92a4ce09ef.png#">https://img-blog.csdnimg.cn/1ea8f6150a2b4ff3999a9d92a4ce09ef.png#</a> &#x3D;60%x)</p>
<h2 id="3-2-按名称分组，并按消费日期累加"><a href="#3-2-按名称分组，并按消费日期累加" class="headerlink" title="3.2  按名称分组，并按消费日期累加"></a>3.2  按名称分组，并按消费日期累加</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span><br>    name,orderdate,cost,<br>    <span class="hljs-built_in">SUM</span>(cost) <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> name <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate)<br><span class="hljs-keyword">FROM</span> testover;<br></code></pre></td></tr></table></figure>
<p><strong>运行结果：</strong><br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/a0b967bfdd1443ad91c0262ccc60d8b8.png#">https://img-blog.csdnimg.cn/a0b967bfdd1443ad91c0262ccc60d8b8.png#</a> &#x3D;60%x)</p>
<h2 id="3-3-在上一个基础上，添加由起点到当前行的聚合"><a href="#3-3-在上一个基础上，添加由起点到当前行的聚合" class="headerlink" title="3.3  在上一个基础上，添加由起点到当前行的聚合"></a>3.3  在上一个基础上，添加由起点到当前行的聚合</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span><br>    name,orderdate,cost,<br>    <span class="hljs-built_in">SUM</span>(cost) <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> name <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate <span class="hljs-keyword">rows</span> <span class="hljs-keyword">BETWEEN</span> UNBOUNDED PRECEDING <span class="hljs-keyword">AND</span> <span class="hljs-keyword">CURRENT</span> <span class="hljs-type">row</span>)<br><span class="hljs-keyword">FROM</span> testover;<br></code></pre></td></tr></table></figure>
<p>运行结果和上一个相同，因为业务场景就是在求同一个窗口所有行的顾客花费</p>
<p>这里应用到了ROWS BETWEEN … AND 语句。</p>
<h1 id="需求四：查看顾客上次的购买时间"><a href="#需求四：查看顾客上次的购买时间" class="headerlink" title="需求四：查看顾客上次的购买时间"></a>需求四：查看顾客上次的购买时间</h1><hr>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> name, orderdate, cost,<br>    <span class="hljs-built_in">LAG</span>(orderdate, <span class="hljs-number">1</span>, orderdate) <span class="hljs-keyword">OVER</span>(<span class="hljs-keyword">partition</span> <span class="hljs-keyword">by</span> name <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate) <span class="hljs-keyword">as</span> t1<br><span class="hljs-keyword">FROM</span> testover;<br></code></pre></td></tr></table></figure>

<p><strong>运行结果：</strong><br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/94f6b4fc21ad45cfb0807e2bedd03cc0.png#">https://img-blog.csdnimg.cn/94f6b4fc21ad45cfb0807e2bedd03cc0.png#</a> &#x3D;60%x)</p>
<h1 id="需求五：查询前20-时间的订单信息"><a href="#需求五：查询前20-时间的订单信息" class="headerlink" title="需求五：查询前20%时间的订单信息"></a>需求五：查询前20%时间的订单信息</h1><hr>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> <br>    (<br>    <span class="hljs-keyword">SELECT</span> name,orderdate,cost,<br>    <span class="hljs-built_in">ntile</span>(<span class="hljs-number">5</span>) <span class="hljs-keyword">over</span>(<span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> orderdate) sorted <span class="hljs-keyword">from</span> testover<br>    ) t<br><span class="hljs-keyword">WHERE</span> sorted <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br></code></pre></td></tr></table></figure>

<p><strong>运行结果：</strong><br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/c7e6168e92294a63b9cf220c73351659.png#">https://img-blog.csdnimg.cn/c7e6168e92294a63b9cf220c73351659.png#</a> &#x3D;60%x)</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%95%B0%E6%8D%AE%E4%BB%93%E5%BA%93/" class="category-chain-item">数据仓库</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Hive/">#Hive</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Hive 笔记2 使用窗口函数over实现电商常见五个需求_ 查询某个时期购买过的顾客以及总人数_ 顾客购买明细以及总额 _ 按照日期对花费进行累加 _查询顾客上次购买时间 _ 查询前20%时间</div>
      <div>http://example.com/2022/03/25/Hive 笔记2 使用窗口函数over实现电商常见五个需求_ 查询某个时期购买过的顾客以及总人数_ 顾客购买明细以及总额 _ 按照日期对花费进行累加 _查询顾客上次购买时间 _ 查询前20%时间/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年3月25日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/03/26/Hive%20%E7%AC%94%E8%AE%B03%20%E4%BD%BF%E7%94%A8Java%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89SQL%E5%87%BD%E6%95%B0%20_%20Hive%E4%B8%89%E5%A4%A7%E7%B1%BB%E5%9E%8B%E5%87%BD%E6%95%B0%E7%9A%84%E7%AE%80%E8%A6%81%E5%8C%BA%E5%88%86%20UDF%E3%80%81UDAF%E3%80%81UDTF/" title="Hive 笔记3 使用Java实现自定义SQL函数 _ Hive三大类型函数的简要区分 UDF、UDAF、UDTF">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Hive 笔记3 使用Java实现自定义SQL函数 _ Hive三大类型函数的简要区分 UDF、UDAF、UDTF</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/03/23/Hive%20%E7%AC%94%E8%AE%B01%20DDL%E6%95%B0%E6%8D%AE%E5%AE%9A%E4%B9%89%E6%93%8D%E4%BD%9C%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%20_%20%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E3%80%81%E6%95%B0%E6%8D%AE%E8%A1%A8%E3%80%81%E5%88%97/" title="Hive 笔记1 DDL数据定义操作常用命令 _ 增删改查数据库、数据表、列">
                        <span class="hidden-mobile">Hive 笔记1 DDL数据定义操作常用命令 _ 增删改查数据库、数据表、列</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
